package com.meirengu.webview.service.impl;

import com.meirengu.channel.service.FangGoldService;
import com.meirengu.common.RedisClient;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.DateUtils;
import com.meirengu.utils.NumberUtil;
import com.meirengu.webview.common.UserChannelEnum;
import com.meirengu.webview.dao.KwInterfaceConfigDao;
import com.meirengu.webview.model.*;
import com.meirengu.webview.service.KwFjsFilterService;
import com.meirengu.webview.service.KwFjsPushService;
import com.meirengu.webview.service.KwXbbPushService;
import com.meirengu.webview.service.UserInfoService;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import static com.meirengu.webview.common.ConfigConsts.CHANNEL_CODE_JUEDAI;
import static com.meirengu.webview.common.ConfigConsts.CHANNEL_NAME_JUEDAI;
import static com.meirengu.webview.common.Constants.BUSINESS_STATUS_SUCCESS;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

/**
 * KwFjsFilter服务实现层
 *
 * @author Mao
 * @create Mon May 07 10:22:14 CST 2018
 */
@Service
public class KwFjsFilterServiceImpl extends BaseServiceImpl<KwFjsFilter> implements KwFjsFilterService {

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private KwInterfaceConfigDao kwInterfaceConfigDao;

    @Autowired
    private FangGoldService fangGoldService;

    @Autowired
    private KwFjsPushService kwFjsPushService;

    @Autowired
    private KwXbbPushService kwXbbPushService;

    //贷款期限选项
    private String[] loanTermStrs = new String[]{"1", "2", "3", "6"};

    Logger logger = LoggerFactory.getLogger(KwFjsFilterServiceImpl.class);


    /**
     * 发送房金所接口
     * 过滤条件
     * 1大于10w
     * 2城市 上海、北京、杭州、深圳、郑州
     * 3公积金，保单，信用卡3其一
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendFangGold(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {
            String fjsIol = redisClient.get("iol-fjs");
            if ("N".equals(fjsIol)) {
                /*//校验是否有熠巨发送成功的记录
                Map<String, Object> xbbParams = new HashMap<>();
                xbbParams.put("mobile", user.getMobile());
                xbbParams.put("channelCode","xbb2");
                xbbParams.put("code", 1);
                Integer xbbPushCount = kwXbbPushService.getCount(xbbParams);
                //已有成功发送记录
                if (!NumberUtil.isNullOrZero(xbbPushCount)) {
                    return sendFlag;
                }*/
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                String city = userInfo.getScity();

                BigDecimal expectAmount = userInfo.getExpectAmount();
                Date brithdayDate = userInfo.getBirthday();
                Integer sex = user.getSex();
                Integer housingType = userInfo.getHouse();
                Integer hasCar = userInfo.getCar();
                Integer housingFund = userInfo.getFund();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer profession = userInfo.getOccupation();
                Integer income = userInfo.getSalary();
                Integer creditSituation = userInfo.getCredit();
                Integer paytype = userInfo.getSalaryMethod();
                Integer socialSecurity = userInfo.getSocialSecurity();
                int a = 1 / 0;
                int age = 25;
                age = DateUtils.getAge(brithdayDate);

                KwFjsFilter kwFjsFilter = new KwFjsFilter();
                kwFjsFilter.setUserId(id);
                kwFjsFilter.setMobile(mobile);
                kwFjsFilter.setChannelCode("fjs");
                kwFjsFilter.setChannelName("房金所");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("fjs");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwFjsFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwFjsFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }

                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwFjsFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        if (kwInterfaceConfig.getFundValue().contains(housingFund.intValue() + "") ||
                                kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "") ||
                                kwInterfaceConfig.getCreditValue().contains(creditSituation.intValue() + "")) {

                            Integer code = fangGoldService.applyLoan(mobile, name, expectAmount.toString(),
                                    city, null);
                            logger.info("发送房金所接口：{}", mobile);
                            kwFjsFilter.setSuited("Y");
                            kwFjsFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, "fjs", user);
                            if (code.intValue() == BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_FUND.getValue());
                            kwFjsFilter.setRefuseMsg("用户无公积金：" + housingFund.intValue() + " 无保单：" + lifeInsurance.intValue() + " 无信用卡：" + creditSituation.intValue());
                        }
                    }
                }

                insert(kwFjsFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    @Override
    public boolean sendYiJu(UserInfo userInfo, User user) throws Exception {
        boolean sendFlag = false;
        try {

            String yuIol = redisClient.get("iol-yiju");
            if ("N".equals(yuIol)) {
                String id = user.getId();
                String name = user.getName();
                if (name.contains("·")) {
                    name = name.replace("·", "");
                }
                String mobile = user.getMobile();
                String city = userInfo.getScity();

                BigDecimal expectAmount = userInfo.getExpectAmount();
                Date brithdayDate = userInfo.getBirthday();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String birthDay = sdf.format(brithdayDate);
                Integer sex = user.getSex();
                String sexStr = "男";
                if (sex == 1) {
                    sexStr = "女";
                }
                Integer housingType = userInfo.getHouse();
                Integer hasCar = userInfo.getCar();
                Integer housingFund = userInfo.getFund();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer profession = userInfo.getOccupation();
                Integer income = userInfo.getSalary();
                Integer creditSituation = userInfo.getCredit();
                Integer paytype = userInfo.getSalaryMethod();
                Integer socialSecurity = userInfo.getSocialSecurity();
                int age = 25;
                age = DateUtils.getAge(brithdayDate);

                KwFjsFilter kwFjsFilter = new KwFjsFilter();
                kwFjsFilter.setUserId(id);
                kwFjsFilter.setMobile(mobile);
                kwFjsFilter.setChannelCode("yiju");
                kwFjsFilter.setChannelName("熠巨");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("yiju");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwFjsFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwFjsFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }

                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwFjsFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        if (kwInterfaceConfig.getFundValue().contains(housingFund.intValue() + "") ||
                                kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "") ||
                                kwInterfaceConfig.getCreditValue().contains(creditSituation.intValue() + "")) {

                            Integer code = fangGoldService.applyLoanYiJu(mobile, name, birthDay, sexStr);
                            logger.info("发送熠巨接口：{}", mobile);
                            kwFjsFilter.setSuited("Y");
                            kwFjsFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, "yiju", user);
                            if (code.intValue() == BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_FUND.getValue());
                            kwFjsFilter.setRefuseMsg("用户无公积金：" + housingFund.intValue() + " 无保单：" + lifeInsurance.intValue() + " 无信用卡：" + creditSituation.intValue());
                        }
                    }
                }

                insert(kwFjsFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    @Override
    public boolean sendYiJuFz(UserInfo userInfo, User user) throws Exception {
        boolean sendFlag = false;
        try {
            //校验是否有熠巨发送成功的记录
            Map<String, Object> yijuParams = new HashMap<>();
            yijuParams.put("mobile", user.getMobile());
            yijuParams.put("channelCode", "yiju");
            yijuParams.put("code", 1);
            Integer yijuPushCount = kwFjsPushService.getCount(yijuParams);
            //已有成功发送记录
            if (!NumberUtil.isNullOrZero(yijuPushCount)) {
                return sendFlag;
            }

            // 校验是否有新旦成功记录
            Map<String, Object> xbbParams = new HashMap<>();
            xbbParams.put("mobile", user.getMobile());
            xbbParams.put("code", 1);
            Integer xbbPushCount = kwXbbPushService.getCount(yijuParams);
            //已有成功发送记录
            if (!NumberUtil.isNullOrZero(xbbPushCount)) {
                return sendFlag;
            }

            String yuIol = redisClient.get("iol-yijufz");
            if ("N".equals(yuIol)) {
                String id = user.getId();
                String name = user.getName();
                if (name.contains("·")) {
                    name = name.replace("·", "");
                }
                String mobile = user.getMobile();
                String city = userInfo.getScity();

                BigDecimal expectAmount = userInfo.getExpectAmount();
                Date brithdayDate = userInfo.getBirthday();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String birthDay = sdf.format(brithdayDate);
                Integer sex = user.getSex();
                String sexStr = "男";
                if (sex == 1) {
                    sexStr = "女";
                }
                Integer housingType = userInfo.getHouse();
                Integer hasCar = userInfo.getCar();
                Integer housingFund = userInfo.getFund();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer profession = userInfo.getOccupation();
                Integer income = userInfo.getSalary();
                Integer creditSituation = userInfo.getCredit();
                Integer paytype = userInfo.getSalaryMethod();
                Integer socialSecurity = userInfo.getSocialSecurity();
                int age = 25;
                age = DateUtils.getAge(brithdayDate);

                KwFjsFilter kwFjsFilter = new KwFjsFilter();
                kwFjsFilter.setUserId(id);
                kwFjsFilter.setMobile(mobile);
                kwFjsFilter.setChannelCode("yijufz");
                kwFjsFilter.setChannelName("熠巨方正");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("yijufz");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwFjsFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwFjsFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }

                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwFjsFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        if (kwInterfaceConfig.getFundValue().contains(housingFund.intValue() + "") ||
                                kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "") ||
                                kwInterfaceConfig.getCreditValue().contains(creditSituation.intValue() + "")) {

                            Integer code = fangGoldService.applyLoanYiJuFz(mobile, name, birthDay, sexStr);
                            logger.info("发送熠巨方正接口：{}", mobile);
                            kwFjsFilter.setSuited("Y");
                            kwFjsFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, "yijufz", user);
                            if (code.intValue() == BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_FUND.getValue());
                            kwFjsFilter.setRefuseMsg("用户无公积金：" + housingFund.intValue() + " 无保单：" + lifeInsurance.intValue() + " 无信用卡：" + creditSituation.intValue());
                        }
                    }
                }

                insert(kwFjsFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    @Override
    public boolean sendOldYiJuFz(UserInfo userInfo, User user) throws Exception {
        boolean sendFlag = false;
        try {
            String yuIol = redisClient.get("iol-yijufz");
            if ("N".equals(yuIol)) {
                String id = user.getId();
                String name = user.getName();
                if (name.contains("·")) {
                    name = name.replace("·", "");
                }
                String mobile = user.getMobile();
                String city = userInfo.getScity();

                BigDecimal expectAmount = userInfo.getExpectAmount();
                Date brithdayDate = userInfo.getBirthday();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String birthDay = sdf.format(brithdayDate);
                Integer sex = user.getSex();
                String sexStr = "男";
                if (sex == 1) {
                    sexStr = "女";
                }
                Integer housingType = userInfo.getHouse();
                Integer hasCar = userInfo.getCar();
                Integer housingFund = userInfo.getFund();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer profession = userInfo.getOccupation();
                Integer income = userInfo.getSalary();
                Integer creditSituation = userInfo.getCredit();
                Integer paytype = userInfo.getSalaryMethod();
                Integer socialSecurity = userInfo.getSocialSecurity();
                int age = 25;
                age = DateUtils.getAge(brithdayDate);

                KwFjsFilter kwFjsFilter = new KwFjsFilter();
                kwFjsFilter.setUserId(id);
                kwFjsFilter.setMobile(mobile);
                kwFjsFilter.setChannelCode("yijufz");
                kwFjsFilter.setChannelName("熠巨方正");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("yijufz");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwFjsFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwFjsFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }

                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwFjsFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        if (kwInterfaceConfig.getFundValue().contains(housingFund.intValue() + "") ||
                                kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "") ||
                                kwInterfaceConfig.getCreditValue().contains(creditSituation.intValue() + "")) {

                            Integer code = fangGoldService.applyLoanYiJuFz(mobile, name, birthDay, sexStr);
                            logger.info("发送熠巨方正接口：{}", mobile);
                            kwFjsFilter.setSuited("Y");
                            kwFjsFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, "yijufz", user);
                            if (code.intValue() == BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_FUND.getValue());
                            kwFjsFilter.setRefuseMsg("用户无公积金：" + housingFund.intValue() + " 无保单：" + lifeInsurance.intValue() + " 无信用卡：" + creditSituation.intValue());
                        }
                    }
                }

                insert(kwFjsFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    @Override
    public boolean sendMdzjYiJuFz(LmUser lmUser) throws Exception {
        boolean sendFlag = false;
        try {
            String yuIol = redisClient.get("iol-yijufz");
            if ("N".equals(yuIol)) {
                Integer id = lmUser.getId();
                String name = lmUser.getName();
                if (name.contains("·")) {
                    name = name.replace("·", "");
                }
                String mobile = lmUser.getPhone();
                String city = lmUser.getAreaName();

                BigDecimal expectAmount = lmUser.getExpectAmount();
                Date brithdayDate = lmUser.getBirthday();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String birthDay = sdf.format(brithdayDate);
                Integer sex = lmUser.getSex();
                String sexStr = "男";
                if (sex == 1) {
                    sexStr = "女";
                }
                Integer housingType = lmUser.getHouse();
                Integer hasCar = lmUser.getCar();
                Integer housingFund = lmUser.getFund();
                Integer lifeInsurance = lmUser.getPolicy();
                Integer profession = lmUser.getOccupation();
                Integer income = lmUser.getSalary();
                Integer creditSituation = lmUser.getCredit();
                Integer paytype = lmUser.getSalaryMethod();
                Integer socialSecurity = lmUser.getSocialSecurity();
                int age = 25;
                age = DateUtils.getAge(brithdayDate);

                KwFjsFilter kwFjsFilter = new KwFjsFilter();
                kwFjsFilter.setUserId(id.toString());
                kwFjsFilter.setMobile(mobile);
                kwFjsFilter.setChannelCode("yijufz");
                kwFjsFilter.setChannelName("熠巨方正");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("yijufz");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwFjsFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwFjsFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }

                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwFjsFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        if (kwInterfaceConfig.getFundValue().contains(housingFund.intValue() + "") ||
                                kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "") ||
                                kwInterfaceConfig.getCreditValue().contains(creditSituation.intValue() + "")) {

                            Integer code = fangGoldService.applyLoanYiJuFz(mobile, name, birthDay, sexStr);
                            logger.info("发送熠巨方正接口：{}", mobile);
                            kwFjsFilter.setSuited("Y");
                            kwFjsFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            User user = new User();
                            user.setMobile(lmUser.getPhone());
                            user.setCreateDate(lmUser.getCreateDate());
                            userInfoService.updateLimitNum(code, "yijufz", user);
                            if (code.intValue() == BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_FUND.getValue());
                            kwFjsFilter.setRefuseMsg("用户无公积金：" + housingFund.intValue() + " 无保单：" + lifeInsurance.intValue() + " 无信用卡：" + creditSituation.intValue());
                        }
                    }
                }

                insert(kwFjsFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    @Override
    public boolean sendJuedai(UserInfo userInfo, User user) {
        boolean sendFlag = false;
        try {
            String cyygIol = redisClient.get("iol-" + CHANNEL_CODE_JUEDAI);
            if ("N".equals(cyygIol)) {
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                String userAgent = user.getUserAgent();
                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer occupation = userInfo.getOccupation();
                Integer wxloan = userInfo.getWxloan();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer credit = userInfo.getCredit();
                Integer house = userInfo.getHouse();
                Integer car = userInfo.getCar();
                String city = userInfo.getScity();
                Integer salary = userInfo.getSalary();
                Integer workDuration = userInfo.getWorkDuration();
                Integer salaryMethod = userInfo.getSalaryMethod();
                Integer socialSecurity = userInfo.getSocialSecurity();
                Integer fund = userInfo.getFund();
                String ip = userInfo.getIp();

                Date brithdayDate = userInfo.getBirthday();
                Integer age = DateUtils.getAge(brithdayDate);
                KwFjsFilter kwFjsFilter = new KwFjsFilter();
                kwFjsFilter.setUserId(id);
                kwFjsFilter.setMobile(mobile);
                kwFjsFilter.setChannelCode(CHANNEL_CODE_JUEDAI);
                kwFjsFilter.setChannelName(CHANNEL_NAME_JUEDAI);

                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode(CHANNEL_CODE_JUEDAI);

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwFjsFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwFjsFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwFjsFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitHouse())) {
                        if (!kwInterfaceConfig.getHouseValue().contains(house.intValue() + "")) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwFjsFilter.setRefuseMsg("没有房：" + house.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitCar())) {
                        if (!kwInterfaceConfig.getCarValue().contains(car.intValue() + "")) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwFjsFilter.setRefuseMsg("没有车：" + car.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitPolicy())) {
                        if (!kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {
                            dataFlag = false;
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwFjsFilter.setRefuseMsg("没有寿险：" + lifeInsurance.intValue());
                        }
                    }

                    if (dataFlag) {
                        if (kwInterfaceConfig.getHouseValue().contains(house.intValue() + "") ||
                                kwInterfaceConfig.getCarValue().contains(car.intValue() + "") ||
                                kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {

                            //以分为单位，所以乘以100
                            String loanStartLimit = expectAmount.multiply(new BigDecimal(100)).toBigInteger().toString();

                            String loanTerm = loanTermStrs[new Random().nextInt(4)];

                            String identityType = "3";
                            if (occupation == null) {
                            } else if (occupation == 0) {
                                identityType = "0";
                            } else if (occupation == 1) {
                                identityType = "2";
                            } else if (occupation == 2) {
                                identityType = "1";
                            }

                            String isWeiliD = "0";
                            if (wxloan != null && wxloan == 1) {
                                isWeiliD = "1";
                            }

                            String isInsurance = "0";
                            if (lifeInsurance != null && (lifeInsurance == 2 || lifeInsurance == 3)) {
                                isInsurance = "1";
                            }

                            String isCreditCard = "0";
                            if (credit != null && credit == 1) {
                                isCreditCard = "2";
                            }

                            String property = "0";
                            if (house != null && (house == 2 || house == 3)) {
                                property = "1";
                            }

                            String carProduction = "0";
                            if (car != null && (car == 2 || car == 3)) {
                                carProduction = "1";
                            }

                            String income = "5000";
                            if (salary == null) {
                            } else if (salary == 1) {
                                income = "3000";
                            } else if (salary == 2) {
                                income = "5000";
                            } else if (salary == 3) {
                                income = "8000";
                            } else if (salary == 4) {
                                income = "10000";
                            } else if (salary == 5) {
                                income = "12000";
                            }

                            String workYears = "1";
                            if (workDuration != null && workDuration == 24) {
                                workYears = "2";
                            }

                            String businessLife = "0";
                            if (salaryMethod != null && salaryMethod == 2) {
                                businessLife = "1";
                            }

                            String isSecurity = "0";
                            if (socialSecurity == null) {
                            } else if (socialSecurity == 1) {
                                isSecurity = "2";
                            } else if (socialSecurity == 2) {
                                isSecurity = "3";
                            }

                            String isFund = "0";
                            if (fund == null) {
                            } else if (fund == 1) {
                                isFund = "2";
                            } else if (fund == 2) {
                                isFund = "3";
                            }

                            String operationYears = "";
                            String isLicense = "";
                            if (occupation != null && occupation == 1) {
                                isLicense = "1";
                                if (workDuration != null && workDuration == 24) {
                                    operationYears = "2";
                                } else {
                                    operationYears = "1";
                                }
                            }

                            //0未安卓，1为苹果
                            String flag = "0";
                            if (isNotEmpty(userAgent) && userAgent.contains("iphone")) {
                                flag = "1";
                            }

                            Integer code = fangGoldService.applyJuedai(mobile, name, "", loanStartLimit, loanTerm, "5", identityType, isWeiliD
                                    , isInsurance, isCreditCard, property, carProduction, city, income, workYears, businessLife
                                    , isSecurity, isFund, operationYears, isLicense, flag, ip);
                            kwFjsFilter.setSuited("Y");
                            kwFjsFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, CHANNEL_CODE_JUEDAI, user);
                            if (code.intValue() == BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            kwFjsFilter.setSuited("N");
                            kwFjsFilter.setCode(UserChannelEnum.FAIL_FUND.getValue());
                            kwFjsFilter.setRefuseMsg("无保单：" + lifeInsurance + " 无房：" + house + " 无车：" + car);
                        }
                    }
                }
                insert(kwFjsFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

}
